package com.zqxq.lottery.controller.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.entity.bo.sys.AddSysActivityBO;
import com.zqxq.lottery.entity.bo.sys.SysActivityBO;
import com.zqxq.lottery.entity.bo.sys.SysCouponBO;
import com.zqxq.lottery.entity.enums.ActivityIdEnum;
import com.zqxq.lottery.service.sys.ISysActivityService;
import com.zqxq.lottery.service.sys.ISysCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 苦智
 * @date 2018/5/23 12:03
 * @description 用户活动
 * @ClassName UserActivityController
 */
@RestController
@Slf4j
@RequestMapping("/activity")
public class SysActivityController extends SysBaseController{

    @Autowired
    private ISysActivityService sysActivityService;

    @Autowired
    private ISysCouponService sysCouponService;

    /**
    * @Author 苦智
    * @Description 用户邀请页面数据展示
    * @Date 17:00 2018/5/24
    * @Param
    * @return
    **/
    @PostMapping("/queryUserActivityData")
    public Result queryUserActivityStatistic(Long userId){
        if (userId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        Map<String,Object> resultMap = sysActivityService.queryUserActivityStatistic(userId);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),resultMap);
    }

    /**
    * @Author 苦智
    * @Description 用户邀请数据排行榜
    * @Date 16:24 2018/6/21
    * @Param []
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/getInviteRankData")
    public Result getInviteRankData(){
        Map<String,Object> resultMap = sysActivityService.getInviteRankData();
        return new Result(CodeEnum.SUCCESS.getCode(),"查询成功",resultMap);
    }

    /**
    * @Author 苦智
    * @Description 用户优惠券展示
    * @Date 17:01 2018/5/24
    * @Param status1:未使用 2:已失效 3:待派发
    * @return
    **/
    @PostMapping("/couponList")
    public Result queryUserCouponList(Long userId,Integer status,Integer pageNo,Integer pageSize){
        log.info("用户优惠券展示,参数userId={},status={}",userId,status);
        if (userId == null || status == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        Page<Map<String,Object>> page = sysActivityService.queryUserCouponList(userId,status,pageNo,pageSize);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),page);
    }

    @PostMapping("/getAvailableCouponList")
    public Result getAvailableCouponList(Long orderId){
        log.info("用户优惠券展示,orderId={}",orderId);
        if (orderId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        List<Map<String,Object>> list = sysActivityService.getAvailableCouponList(orderId);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),list);
    }
    @PostMapping("/updateCouponUsed")
    public Result updateCouponUsed(Long userId,Long couponId){
        sysActivityService.updateCouponUsed(userId,couponId);
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
    }


    /**
    * @Author 苦智
    * @Description 支付优惠券抵扣
    * @Date 18:33 2018/5/25
    * @Param [userId, couponId] 用户id，优惠券id
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/couponConsume")
    public Result couponConsume(Long couponId,Long orderId){
        Result result = new Result();
        log.info("couponConsume couponId={},orderId={},",couponId,orderId);
        if (orderId == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        Map<String,Object> payAmount = sysActivityService.saveCouponConsume(couponId,orderId);
        if (payAmount != null){
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg("支付页面数据获取成功");
            result.setData(payAmount);
            return result;
        }
        result.setCode(CodeEnum.FAIL.getCode());
        result.setMsg(CodeEnum.FAIL.getMsg());
        return result;
    }

    @PostMapping("/querySysActivity")
    public Result querySysActivity(@RequestBody Map<String,Object> paramMap){
        Result rs = new Result();
        try{
            Page page = sysActivityService.querySysActivity(paramMap);
            rs.setData(page);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Query sysActivity Error:",e);
        }
        return rs;
    }
    @PostMapping("/saveSysActivity")
    public Result saveSysActivity(@RequestBody AddSysActivityBO bo){
        Result result = new Result();
        AddSysActivityBO returnBo = sysActivityService.saveSysActivity(bo);
        if (returnBo != null){
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg("保存活动成功！");
            result.setData(returnBo);
            return result;
        }
        result.setCode(CodeEnum.FAIL.getCode());
        result.setMsg(CodeEnum.FAIL.getMsg());
        return result;
    }
    @PostMapping("/deleteSysActivity")
    public Result deleteSysActivity(@RequestParam String activityId){
        Result result = new Result();
        sysActivityService.deleteSysActivity(activityId);
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg("删除活动成功！");
        return result;
    }
    @PostMapping("/saveSysCoupon")
    public Result saveSysCoupon(@RequestBody SysCouponBO bo){
        Result result = new Result();
        SysCouponBO returnBo = sysActivityService.saveSysCoupon(bo);
        if (returnBo != null){
            result.setCode(CodeEnum.SUCCESS.getCode());
            result.setMsg("新增活动成功！");
            result.setData(returnBo);
            return result;
        }
        result.setCode(CodeEnum.FAIL.getCode());
        result.setMsg(CodeEnum.FAIL.getMsg());
        return result;
    }
    @PostMapping("/deleteSysCoupon")
    public Result deleteSysCoupon(@RequestParam String couponId){
        Result result = new Result();
        sysActivityService.deleteSysCoupon(couponId);
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg("删除活动优惠券成功！");
        return result;
    }
    @PostMapping("/querySysCoupon")
    public Result querySysCoupon(@RequestBody Map<String,Object> paramMap){
        Result rs = new Result();
        try{
            Page page = sysActivityService.querySysCoupon(paramMap);
            rs.setData(page);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("Query sysCoupon Error:",e);
        }
        return rs;
    }

    @PostMapping("/acceptingCoupon")
    public Result acceptingCoupon(){
        Result result = new Result();
        sysActivityService.acceptingCoupon();
        result.setCode(CodeEnum.SUCCESS.getCode());
        return result;
    }
    @PostMapping("/updateUserCoupon")
    public Result updateUserCoupon(){
        Result result = new Result();
        sysCouponService.updateUserCoupon();
        result.setCode(CodeEnum.SUCCESS.getCode());
        return result;
    }
}
